package com.cj.controller;
import com.cj.service.userService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by chenjie on 2017/5/3.
 */
@Controller
@RequestMapping("/home")
public class homeController {
    private static final Logger logger =LoggerFactory.getLogger(homeController.class);
    @Autowired
    private userService userService;

    @RequestMapping(value ="/index")
    public  String index(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            System.out.println(cookies[i].getName());
            System.out.println(cookies[i].getValue());
        }
        String host = request.getServerName();
        Cookie cookie = new Cookie("SESSION_LOGIN_USERNAME", "ads"); // 保存用户名到Cookie
        cookie.setPath("/");
        cookie.setDomain(host);
        cookie.setMaxAge(99999999);
        response.addCookie(cookie);
        logger.info("index---------");
        userService.select();
        return "index";
    }

    @RequestMapping(value ="/test",method = RequestMethod.POST)
    public  @ResponseBody  String test(HttpServletRequest request, HttpServletResponse response){

        return "okkkk";
    }

    @RequestMapping(value ="/login",method = RequestMethod.POST)
    public  @ResponseBody String login(HttpServletRequest request, HttpServletResponse response,String username, String password){
        logger.info("login----用户名:"+username+"---密码:"+password);
        HttpSession session = request.getSession();
        session.setAttribute("loginname",username);
//        String host = request.getServerName();
//        Cookie cookie = new Cookie("SESSION_LOGIN_USERNAME", username); // 保存用户名到Cookie
//        cookie.setPath("/");
//        cookie.setDomain(host);
//        cookie.setMaxAge(99999999);
//        response.addCookie(cookie);
//        userService.select();
          return session.getId();
    }
}
